/**********************************
 *** Imports
 **********************************/
package fr.montreuil.wargame.controller;

import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import fr.montreuil.wargame.domain.User;
import fr.montreuil.wargame.repository.TechnologieRepository;
import fr.montreuil.wargame.repository.UserRepository;
import fr.montreuil.wargame.repository.BatimentRepository;

/**********************************
 *** @jgay
 *** @30 avr. 2012
 **********************************/
@Controller
@RequestMapping(value = "/admin")
public class AdminController {

	// --------------------------------------------------------------------------
	// Les repositories
	// --------------------------------------------------------------------------

	@Resource
	UserRepository			userRepository;
	@Resource
	BatimentRepository		batimentRepository;
	@Resource
	TechnologieRepository	technologieRepository;

	// --------------------------------------------------------------------------
	// Page d'acceuil admin
	// --------------------------------------------------------------------------

	@RequestMapping
	public String getHomePageAdmin() {
		return "admin";
	}

	// --------------------------------------------------------------------------
	// Pour les users
	// --------------------------------------------------------------------------

	@RequestMapping(value = "/user/view")
	public String view(Model model) {
		model.addAttribute("users", userRepository.findAll());

		return "admin/user/view";
	}

	@RequestMapping(method = RequestMethod.GET, value = "/user/modify")
	public String modifyGET(Model model,
			@RequestParam(required = true) String username,
			@RequestParam(required = false) String message) {
		model.addAttribute("message", message);
		model.addAttribute("user", userRepository.findByUsername(username));

		return "admin/user/modify";
	}

	@RequestMapping(method = RequestMethod.POST, value = "/user/modify")
	public String modifyPOST(@RequestParam String username,
			@RequestParam String firstname, 
			@RequestParam String lastname,
			@RequestParam String lvl, 
			@RequestParam String exp) {
		String message = "";

		// Si le username est vide
		if (username.isEmpty() || username == "") {
			message = "Username vide !";
			// Si le poassword est vide
		} else if (firstname.isEmpty() || firstname == "") {
			message = "FirstName vide !";
			// Si le lastname est vide
		} else if (lastname.isEmpty() || lastname == "") {
			message = "LastName vide !";
		} else if (lvl.isEmpty() || lvl == "") {
			message = "Level vide !";
		} else if (exp.isEmpty() || exp == "") {
			message = "Experience vide !";
		}

		// Récuperation d'un user avec l'username
		User exist = userRepository.findByUsername(username);

		// Si il n'y a aucune erreur
		if (message.isEmpty()) {
			exist.setFirstName(firstname);
			exist.setLastName(lastname);
			exist.setLvl(Integer.parseInt(lvl));
			exist.setExp(Integer.parseInt(exp));

			userRepository.save(exist);

			// Redirection
			return "redirect:/admin/user/view";
			// Si il y a une erreur
		} else {
			return "redirect:/admin/user/modify?username=" + username
					+ "&message=" + message;
		}
	}
	
}